<?php
session_start ();
require_once './php_lib/conexion.php';
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$query_Color = "SELECT * FROM Color";
$Color = mysql_query($query_Color, $db_zapatos) or die(mysql_error());
$row_Color = mysql_fetch_assoc($Color);
$totalRows_Color = mysql_num_rows($Color);

$query_Talla = "SELECT * FROM Talla";
$Talla = mysql_query($query_Talla, $db_zapatos) or die(mysql_error());
$row_Talla = mysql_fetch_assoc($Talla);
$totalRows_Talla = mysql_num_rows($Talla);

$query_Modelo = "SELECT mod_id, mod_descripcion FROM Modelo";
$Modelo = mysql_query($query_Modelo, $db_zapatos) or die(mysql_error());
$row_Modelo = mysql_fetch_assoc($Modelo);
$totalRows_Modelo = mysql_num_rows($Modelo);

$query_PerfilCliente = "SELECT perfc_id, perfc_descripcion FROM Perfil_Cliente";
$PerfilCliente = mysql_query($query_PerfilCliente, $db_zapatos) or die(mysql_error());
$row_PerfilCliente = mysql_fetch_assoc($PerfilCliente);
$totalRows_PerfilCliente = mysql_num_rows($PerfilCliente);

$query_Tipo = "SELECT * FROM Tipo";
$Tipo = mysql_query($query_Tipo, $db_zapatos) or die(mysql_error());
$row_Tipo = mysql_fetch_assoc($Tipo);
$totalRows_Tipo = mysql_num_rows($Tipo);
?>
<?php
if(isset($_POST['prod']) 
or (isset($_POST['valor']) && $_POST['valor']!="!=0") 
or (isset($_POST['color']) && $_POST['color']!="!=0")
or (isset($_POST['talla']) && $_POST['talla']!="!=0")
or (isset($_POST['modelo']) && $_POST['modelo']!="!=0")
or (isset($_POST['pfc']) && $_POST['pfc']!="!=0")
or (isset($_POST['tipo']) && $_POST['tipo']!="!=0")){
	if(isset($_POST['prod'])){
		$slike = "AND prod_id=".$_POST['prod'];
	}
	else{
		$slike="";
		if(isset($_POST['valor'])){
			$slike .= " AND Producto.prod_valor ".$_POST['valor'];
		}
		if(isset($_POST['color'])){
			$slike .= " AND Color.col_id ".$_POST['color'];
		}
		if(isset($_POST['talla'])){
			$slike .= " AND Talla.talla_id ".$_POST['talla'];
		}
		if(isset($_POST['modelo'])){
			$slike .= " AND Modelo.mod_id ".$_POST['modelo'];
		}
		if(isset($_POST['pfc'])){
			$slike .= " AND Perfil_Cliente.perfc_id ".$_POST['pfc'];
		}
		if(isset($_POST['tipo'])){
			$slike .= " AND Tipo.tip_id ".$_POST['tipo'];
		}
	}
	$query = "SELECT prod_id,prod_valor,col_descripcion,talla_num,mod_descripcion,perfc_descripcion,tip_descripcion 
FROM Producto,Color,Talla,Modelo,Perfil_Cliente,Tipo
WHERE Producto.col_id = Color.col_id
AND Producto.col_id = Color.col_id
AND Producto.talla_id = Talla.talla_id
AND Producto.mod_id = Modelo.mod_id
AND Producto.perfc_id = Perfil_Cliente.perfc_id
AND Producto.tip_id = Tipo.tip_id ".$slike;
// 
	$queEmp = mysql_query($query, $db_zapatos)or die(mysql_error());
	$rsEmp  = mysql_fetch_assoc($queEmp);
	}
?>
<link href="css/estilos.css" rel="stylesheet" type="text/css" />

<h3><em>Busqueda por ID del producto </em>
</h3>
<form id="form1" name="form1" method="post" action=""> 
  <label for="prod"></label>
    <input type="text" name="prod" id="prod" />
    <input type="button" name="button1" id="button1" value="Buscar"/>
  </p>
</form>
<h3><em>Busqueda avanzada del producto </em>
</h3>
<form id="form2" name="form2" method="post" action="">
  <p>
    <label for="valor">Valor</label>
    <select name="valor" id="valor">
      <option value="!=0" selected="selected">Seleccione Rango</option>
      <option value=" between 20000 AND 25000">$20000-$25000</option>
      <option value=" between 25001 AND 30000">$25001-$30000</option>
      <option value=" between 35001 AND 35000">$30001-$35000</option>
      <option value=" between 40001 AND 45000">$40001-$45000</option>
      <option value=" between 45001 AND 50000">$45001-$50000</option>
    </select>
    <label for="color">Color</label>
    <select name="color" id="color">
      <option value="!=0">Seleccione Color</option>
      <?php
do {  
?>
      <option value=" = <?php echo $row_Color['col_id']?>"><?php echo $row_Color['col_descripcion']?></option>
      <?php
} while ($row_Color = mysql_fetch_assoc($Color));
  $rows = mysql_num_rows($Color);
  if($rows > 0) {
      mysql_data_seek($Color, 0);
	  $row_Color = mysql_fetch_assoc($Color);
  }
?>
    </select>
    <label for="talla">Talla</label>
    <select name="talla" id="talla">
      <option value="!=0">Seleccione Talla</option>
      <?php
do {  
?>
      <option value=" = <?php echo $row_Talla['talla_id']?>"><?php echo $row_Talla['talla_num']?></option>
      <?php
} while ($row_Talla = mysql_fetch_assoc($Talla));
  $rows = mysql_num_rows($Talla);
  if($rows > 0) {
      mysql_data_seek($Talla, 0);
	  $row_Talla = mysql_fetch_assoc($Talla);
  }
?>
    </select>
    </select>
  <label for="modelo"><br>
    Modelo</label>
  <select name="modelo" id="modelo">
    <option value="!=0">Seleccione Talla</option>
    <?php
do {  
?>
    <option value=" = <?php echo $row_Modelo['mod_id']?>"><?php echo $row_Modelo['mod_descripcion']?></option>
    <?php
} while ($row_Modelo = mysql_fetch_assoc($Modelo));
  $rows = mysql_num_rows($Modelo);
  if($rows > 0) {
      mysql_data_seek($Modelo, 0);
	  $row_Modelo = mysql_fetch_assoc($Modelo);
  }
?>
  </select>
  </select>
  <label for="pfc">Perfil Cliente</label>
  <select name="pfc" id="pfc">
    <option value="!=0">Seleccione Talla</option>
    <?php
do {  
?>
    <option value=" = <?php echo $row_PerfilCliente['perfc_id']?>"><?php echo $row_PerfilCliente['perfc_descripcion']?></option>
    <?php
} while ($row_PerfilCliente = mysql_fetch_assoc($PerfilCliente));
  $rows = mysql_num_rows($PerfilCliente);
  if($rows > 0) {
      mysql_data_seek($PerfilCliente, 0);
	  $row_PerfilCliente = mysql_fetch_assoc($PerfilCliente);
  }
?>
  </select>
  </select>
  <label for="tipo">Tipo</label>
  <select name="tipo" id="tipo">
    <option value="!=0">Seleccione Tipo</option>
    <?php
do {  
?>
    <option value=" = <?php echo $row_Tipo['tip_id']?>"><?php echo $row_Tipo['tip_descripcion']?></option>
    <?php
} while ($row_Tipo = mysql_fetch_assoc($Tipo));
  $rows = mysql_num_rows($Tipo);
  if($rows > 0) {
      mysql_data_seek($Tipo, 0);
	  $row_Tipo = mysql_fetch_assoc($Tipo);
  }
?>
  </select>
    <input type="button" name="button2" id="button2" value="Buscar" />
  </p>
</form>
<p>
  <?php if(isset($rsEmp) && $rsEmp!=null){?>
  <em>Total de Resultados: <?php echo mysql_num_rows($queEmp); ?></em>
</p>
<table width="459">
  <tr>
    <th width="73">Producto</th>
    <th width="49">Valor</th>
    <th width="49">Color</th>
    <th width="46">Talla</th>
    <th width="61">Modelo</th>
    <th width="106">Perfil Cliente</th>
    <th width="43">Tipo</th>
  </tr>
  <?php do { ?>
  <tr>
    <td><?php echo $rsEmp['prod_id']; ?></td>
    <td><?php echo $rsEmp['prod_valor']; ?></td>
    <td><?php echo $rsEmp['col_descripcion']; ?></td>
    <td><?php echo $rsEmp['talla_num']; ?></td>
    <td><?php echo $rsEmp['mod_descripcion']; ?></td>
    <td><?php echo $rsEmp['perfc_descripcion']; ?></td>
    <td><?php echo $rsEmp['tip_descripcion']; ?></td>
    <td width="8%"><a href="eliminar.php?per_rut=<?php echo $row_Recordset3['per_rut']; ?>"><img src="imagenes/borrar.gif" width="15" height="17" alt="Eliminar" /></a></td>
  </tr>
  <?php }while ($rsEmp = mysql_fetch_assoc($queEmp));?>
</table>
  <?php }?>

<script language="javascript">
$('#button1').click(function() {
	// Enviamos el formulario usando AJAX
	$.ajax({
	type: 'POST',
	url: 'buscar_producto.php',
	data: $('#form1').serialize(),
	// Mostramos un mensaje con la respuesta de PHP
	success: function(data) {
		$('#mainDiv').html(data);
	}})        
});

$('#button2').click(function() {
	// Enviamos el formulario usando AJAX
	$.ajax({
	type: 'POST',
	url: 'buscar_producto.php',
	data: $('#form2').serialize(),
	// Mostramos un mensaje con la respuesta de PHP
	success: function(data) {
		$('#mainDiv').html(data);
	}})        
});

</script>